package controller

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
	"zhanyan/pac/cache"
	"zhanyan/pac/template"
	"zhanyan/pac/util"
	"zhanyan/service/user_service"
)

// UserIsExist
// @Summary      用户是否存在
// @Description  判断是否用户存在来进行用户登录
// @Tags         user
// @Accept       json
// @Produce      json
// @Param        username   query      string  true  "用户名"
// @Param        password   query      string  true  "密码"
// @Success      200  {object}  map[string]interface{}
// @Router       /user/isExists [get]
func UserIsExist(c *gin.Context) {
	username := c.Query("username")
	password := c.Query("password")
	user := user_service.User{
		Username: username,
		Password: password,
	}

	exist := user.IsExist()

	c.HTML(http.StatusOK, template.TestHtml, map[string]interface{}{
		"isExist": exist,
	})
}

// AddTeacher
// @Summary      添加教师实体
// @Description  添加教师实体
// @Tags         user
// @Accept       json
// @Produce      json
// @Param        username   application/x-www-form-urlencoded      string  true  "用户名"
// @Param        password   application/x-www-form-urlencoded      string  true  "密码"
// @Param        name   application/x-www-form-urlencoded      string  true  "名字"
// @Param        schoolCode   application/x-www-form-urlencoded      string  true  "学校编号"
// @Param        teacherCode application/x-www-form-urlencoded string true "teacher编号"
// @Param        phone   application/x-www-form-urlencoded      string  true  "电话"
// @Success      200  {object}  map[string]interface{}
// @Router       /user/addTeacher [post]
func AddTeacher(c *gin.Context) {
	logE, _ := util.GetLogger(util.Errors)
	teacher := user_service.Teacher{}
	if err := c.ShouldBindJSON(&teacher); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": http.StatusNoContent,
			"msg":  "传参错误",
		})
	}
	if err := teacher.Add(); err != nil {
		logE.Errorf("添加教师失败%v", err)
		c.JSON(http.StatusOK, gin.H{
			"code": http.StatusNoContent,
			"msg":  err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "添加成功",
	})
}

// AddStudent
// @Summary      添加学生实体
// @Description  添加学生实体
// @Tags         user
// @Accept       json
// @Produce      json
// @Param        username   application/x-www-form-urlencoded      string  true  "用户名"
// @Param        password   application/x-www-form-urlencoded      string  true  "密码"
// @Param        name   application/x-www-form-urlencoded      string  true  "名字"
// @Param        schoolCode   application/x-www-form-urlencoded      string  true  "学校编号"
// @Param        studentCode application/x-www-form-urlencoded string true "teacher编号"
// @Success      200  {object}  map[string]interface{}
// @Router       /user/addStudent [post]
func AddStudent(c *gin.Context) {
	logE, _ := util.GetLogger(util.Errors)
	student := user_service.Student{}
	if err := c.ShouldBindJSON(&student); err != nil {
		logE.Errorf("添加学生失败-%v", err)
		c.JSON(http.StatusOK, gin.H{
			"code": http.StatusNoContent,
			"msg":  "传参错误",
		})
		return
	}
	if err := student.Add(); err != nil {
		logE.Errorf("添加学生失败-%v", err)
		c.JSON(http.StatusOK, gin.H{
			"code": http.StatusNoContent,
			"msg":  err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "添加成功",
	})
}

// UpdateStudent
// @Summary      修改学生实体
// @Description  修改学生实体
// @Tags         user
// @Accept       json
// @Produce      json
// @Param        id   application/x-www-form-urlencoded      int  true  "用户名"
// @Param        username   application/x-www-form-urlencoded      string  true  "用户名"
// @Param        password   application/x-www-form-urlencoded      string  true  "密码"
// @Param        name   application/x-www-form-urlencoded      string  true  "名字"
// @Param        schoolCode   application/x-www-form-urlencoded      string  true  "学校编号"
// @Param        studentCode application/x-www-form-urlencoded string true "学生编号"
// @Success      200  {object}  map[string]interface{}
// @Router       /user/updateStudent [put]
func UpdateStudent(c *gin.Context) {
	logE, _ := util.GetLogger(util.Errors)

	student := user_service.Student{}
	if err := c.ShouldBindJSON(&student); err != nil {
		logE.Errorf("修改学生失败-%v", err)
		c.JSON(http.StatusOK, gin.H{
			"code": http.StatusNoContent,
			"msg":  "传参错误",
		})
		return
	}
	if err := student.Update(); err != nil {
		fmt.Println("student.Update()")
		logE.Errorf("修改学生失败-%v", err)
		c.JSON(http.StatusOK, gin.H{
			"code": http.StatusNoContent,
			"msg":  err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "修改成功",
	})
}

// UpdateTeacher
// @Summary      修改教师实体
// @Description  修改教师实体
// @Tags         user
// @Accept       json
// @Produce      json
// @Param        id   application/x-www-form-urlencoded      int  true  "用户名"
// @Param        username   application/x-www-form-urlencoded      string  true  "用户名"
// @Param        password   application/x-www-form-urlencoded      string  true  "密码"
// @Param        name   application/x-www-form-urlencoded      string  true  "名字"
// @Param        schoolCode   application/x-www-form-urlencoded      string  true  "学校编号"
// @Param        teacherCode application/x-www-form-urlencoded string true "teacher编号"
// @Param        phone application/x-www-form-urlencoded string true "teacher编号"
// @Success      200  {object}  map[string]interface{}
// @Router       /user/updateTeacher [put]
func UpdateTeacher(c *gin.Context) {
	logE, _ := util.GetLogger(util.Errors)

	teacher := user_service.Teacher{}
	if err := c.ShouldBindJSON(&teacher); err != nil {
		logE.Errorf("添加学生失败-%v", err)
		c.JSON(http.StatusOK, gin.H{
			"code": http.StatusNoContent,
			"msg":  "传参错误",
		})
		return
	}
	if exist := teacher.IsExistById(); !exist {
		logE.Errorf("修改教师失败-%v", "用户不存在")
		c.JSON(http.StatusOK, gin.H{
			"code": http.StatusNoContent,
			"msg":  "用户不存在",
		})
		return
	}
	if err := teacher.Update(); err != nil {
		logE.Errorf("修改教师失败-%v", err)
		c.JSON(http.StatusOK, gin.H{
			"code": http.StatusNoContent,
			"msg":  err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "修改成功",
	})
}

// DeleteTeacher
// @Summary      删除教师实体
// @Description  删除教师实体
// @Tags         user
// @Accept       json
// @Produce      json
// @Param        id   path      integer   true  "用户id"
// @Success      200  {object}  map[string]interface{}
// @Router       /user/deleteTeacher/:id [delete]
func DeleteTeacher(c *gin.Context) {
	logE, _ := util.GetLogger(util.Errors)
	id, _ := strconv.Atoi(c.Param("id"))
	teacher := user_service.Teacher{User: user_service.User{
		ID: id,
	},
	}

	if err := teacher.Delete(); err != nil {
		logE.Errorf("删除教师失败-%v", err)
		c.JSON(http.StatusOK, gin.H{
			"code": http.StatusNoContent,
			"msg":  err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "ok",
	})
}

// DeleteTeacherInClass
// @Summary      删除教师与教室的关联
// @Description  删除教师与教室的关联
// @Tags         user
// @Accept       json
// @Produce      json
// @Param        tid   query      integer   true  "用户id"
// @Param        cid   query      integer   true  "用户id"
// @Success      200  {object}  map[string]interface{}
// @Router       /user/deleteTeacherInClass [delete]
func DeleteTeacherInClass(c *gin.Context) {
	logE, _ := util.GetLogger(util.Errors)
	tid, _ := strconv.Atoi(c.Query("tid"))
	cid, _ := strconv.Atoi(c.Query("cid"))
	teacher := user_service.Teacher{User: user_service.User{
		ID: tid,
	},
	}

	if err := teacher.DeleteInClass(cid); err != nil {
		logE.Errorf("删除教师与教室的关联-%v", err)
		c.HTML(http.StatusCreated, template.DeleteUserHtml, map[string]interface{}{
			"操作码": err,
		})
		return
	}
	c.HTML(http.StatusOK, template.DeleteUserHtml, map[string]interface{}{
		"操作码": "success",
	})
}

// DeleteTeacherAllClass
// @Summary      删除教师与教室的所有关联
// @Description  删除教师与教室的所有关联
// @Tags         user
// @Accept       json
// @Produce      json
// @Param        tid   query      integer   true  "用户id"
// @Success      200  {object}  map[string]interface{}
// @Router       /user/deleteTeacherAllClass [delete]
func DeleteTeacherAllClass(c *gin.Context) {
	logE, _ := util.GetLogger(util.Errors)
	tid, _ := strconv.Atoi(c.Query("tid"))
	teacher := user_service.Teacher{User: user_service.User{
		ID: tid,
	},
	}

	if err := teacher.DeleteAllClass(); err != nil {
		logE.Errorf("删除教师与教室的所有关联-%v", err)
		c.HTML(http.StatusCreated, template.DeleteUserHtml, map[string]interface{}{
			"操作码": err,
		})
		return
	}
	c.HTML(http.StatusOK, template.DeleteUserHtml, map[string]interface{}{
		"操作码": "success",
	})
}

// DeleteStudent
// @Summary      删除学生实体
// @Description  删除学生实体
// @Tags         user
// @Accept       json
// @Produce      json
// @Param        id   path      integer   true  "用户id"
// @Success      200  {object}  map[string]interface{}
// @Router       /user/deleteStudent/:id [delete]
func DeleteStudent(c *gin.Context) {
	logE, _ := util.GetLogger(util.Errors)
	id, _ := strconv.Atoi(c.Param("id"))
	student := user_service.Student{User: user_service.User{
		ID: id,
	},
	}

	if err := student.Delete(); err != nil {
		logE.Errorf("删除学生失败-%v", err)
		c.JSON(http.StatusOK, gin.H{
			"code": http.StatusNoContent,
			"msg":  err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "ok",
	})
}

// GetStudent
// @Summary      获取学生实体
// @Description  获取学生实体
// @Tags         user
// @Accept       json
// @Produce      json
// @Param        id   path      integer   true  "用户id"
// @Success      200  {object}  map[string]interface{}
// @Router       /user/getStudent/:id [get]
func GetStudent(c *gin.Context) {
	logE, _ := util.GetLogger(util.Errors)
	id, _ := strconv.Atoi(c.Param("id"))
	student := user_service.Student{User: user_service.User{
		ID: id,
	},
	}

	if err := student.Get(); err != nil {
		logE.Errorf("查询学生失败-%v", err)
		c.JSON(http.StatusOK, gin.H{
			"code": http.StatusNoContent,
			"msg":  err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  student,
	})
}

// GetTeacher
// @Summary      获取教师实体
// @Description  获取教师实体
// @Tags         user
// @Accept       json
// @Produce      json
// @Param        id   path      integer   true  "用户id"
// @Success      200  {object}  map[string]interface{}
// @Router       /user/getTeacher/:id [get]
func GetTeacher(c *gin.Context) {
	logE, _ := util.GetLogger(util.Errors)
	id, _ := strconv.Atoi(c.Param("id"))
	teacher := user_service.Teacher{User: user_service.User{
		ID: id,
	},
	}

	if err := teacher.Get(); err != nil {
		logE.Errorf("查询教师失败-%v", err)
		c.JSON(http.StatusOK, gin.H{
			"code": http.StatusNoContent,
			"msg":  err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  teacher,
	})
}

// GetTeacherInClass
// @Summary      获取教师实体并带着教室
// @Description  获取教师实体并带着教室
// @Tags         user
// @Accept       json
// @Produce      json
// @Param        id   path      integer   true  "用户id"
// @Success      200  {object}  map[string]interface{}
// @Router       /user/getTeacherInClass/:id [get]
func GetTeacherInClass(c *gin.Context) {
	logE, _ := util.GetLogger(util.Errors)
	id, _ := strconv.Atoi(c.Param("id"))
	teacher := user_service.Teacher{User: user_service.User{
		ID: id,
	},
	}

	if err := teacher.GetInClass(); err != nil {
		logE.Errorf("查询教师失败-%v", err)
		c.JSON(http.StatusOK, gin.H{
			"code": http.StatusOK,
			"msg":  err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  teacher.Classroom,
	})
}

// GetAllStudent
// @Summary      获取所有学生实体
// @Description  获取所有学生实体
// @Tags         user
// @Accept       json
// @Produce      json
// @Success      200  {object}  map[string]interface{}
// @Router       /user/getAllStudent [get]
func GetAllStudent(c *gin.Context) {
	logE, _ := util.GetLogger(util.Errors)
	gc := cache.StudentCache()
	get, err := gc.Get("students")
	if err != nil {
		logE.Errorf("查询学生失败-%v", err)
		c.HTML(http.StatusCreated, template.QueryUserHtml, map[string]interface{}{
			"操作码": err,
		})
		return
	}
	c.HTML(http.StatusOK, template.QueryUserHtml, map[string]interface{}{
		"操作码":  "success",
		"user": get,
	})
}

// GetAllTeacher
// @Summary      获取所有教师实体
// @Description  获取所有教师实体
// @Tags         user
// @Accept       json
// @Produce      json
// @Success      200  {object}  map[string]interface{}
// @Router       /user/getAllTeacher [get]
func GetAllTeacher(c *gin.Context) {
	logE, _ := util.GetLogger(util.Errors)
	gc := cache.TeacherCache()
	get, err := gc.Get("teachers")
	if err != nil {
		logE.Errorf("查询教师失败-%v", err)
		c.HTML(http.StatusCreated, template.QueryUserHtml, map[string]interface{}{
			"操作码": err,
		})
		return
	}

	c.HTML(http.StatusOK, template.QueryUserHtml, map[string]interface{}{
		"操作码":  "success",
		"user": get,
	})
}

// GetTeacherPage
// @Summary      分页获取所有教师实体
// @Description  分页获取所有教师实体
// @Tags         user
// @Accept       json
// @Produce      json
// @Param        pageSize   query      integer   true  "页面大小"
// @Param        pageCode   query      integer   true  "页面编号"
// @Success      200  {object}  map[string]interface{}
// @Router       /user/getTeacherPage [get]
func GetTeacherPage(c *gin.Context) {
	logE, _ := util.GetLogger(util.Errors)
	teacher := user_service.Teacher{}
	pageSize, _ := strconv.Atoi(c.Query("pageSize"))
	pageCode, _ := strconv.Atoi(c.Query("pageCode"))
	allTeachers, err := teacher.GetPage(pageSize, pageCode)
	if err != nil {
		logE.Errorf("查询教师失败-%v", err)
		c.HTML(http.StatusCreated, template.QueryUserHtml, map[string]interface{}{
			"操作码": err,
		})
		return
	}
	c.HTML(http.StatusOK, template.QueryUserHtml, map[string]interface{}{
		"操作码":  "success",
		"user": *allTeachers,
	})
}

// GetStudentPage
// @Summary      分页获取所有学生实体
// @Description  分页获取所有学生实体
// @Tags         user
// @Accept       json
// @Produce      json
// @Param        pageSize   query      integer   true  "页面大小"
// @Param        pageCode   query      integer   true  "页面编号"
// @Success      200  {object}  map[string]interface{}
// @Router       /user/getStudentPage [get]
func GetStudentPage(c *gin.Context) {
	logE, _ := util.GetLogger(util.Errors)
	student := user_service.Student{}
	pageSize, _ := strconv.Atoi(c.Query("pageSize"))
	pageCode, _ := strconv.Atoi(c.Query("pageCode"))
	allStudents, err := student.GetPage(pageSize, pageCode)
	if err != nil {
		logE.Errorf("查询学生失败-%v", err)
		c.HTML(http.StatusCreated, template.QueryUserHtml, map[string]interface{}{
			"操作码": err,
		})
		return
	}
	c.HTML(http.StatusOK, template.QueryUserHtml, map[string]interface{}{
		"操作码":  "success",
		"user": *allStudents,
	})
}
